<template>
  <div class="login">
    <el-form
      class="logig-form"
      ref="form"
      :model="form"
      :rules="rules"
      label-position="top"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          class="login-btn"
          type="primary"
          :loading="loading"
          @click="onSubmit"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import { login } from '@/services/user';
import { Form } from 'element-ui';
export default Vue.extend({
  name: 'LoginIndex',
  data() {
    return {
      form: {
        phone: '18201288771',
        password: '111111',
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1\d{10}$/,
            message: '请输入正确的手机号',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          {
            min: 6,
            max: 18,
            message: '请输入6-18位密码',
            trigger: 'blur',
          },
        ],
      },
      loading: false,
    };
  },
  methods: {
    onSubmit() {
      (this.$refs.form as Form).validate(async (valite) => {
        if (valite) {
          this.loading = true;
          const { data } = await login(this.form);
          this.loading = false;
          if (data.state !== 1) {
            return this.$message.error(data.message);
          }
          this.$store.commit('setUser', data.content);
          this.$router.push((this.$route.query.redirect as string) || '/');
          // this.$router.push({
          //   name: 'home',
          // });
          this.$message.success('登录成功');
        }
      });
    },
  },
});
</script>

<style lang="scss">
.login {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  .logig-form {
    background: #fff;
    padding: 20px;
    border-radius: 5px;
    width: 300px;
  }
  .login-btn {
    width: 100%;
  }
}
</style>
